//JavaScript代码区域
layui.use(['element', 'table', 'jquery', 'laydate', 'form'], function () {
    var element = layui.element;
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    // var $ = layui.jquery;
    laydate.render({
        elem: "#start_date"
    })
    laydate.render({
        elem: "#end_date"
    });
    $(function () {
        getLeader();
        getCoach();
        tableRender();
        form.render();
    })
    $("#find").click(function () {
        tableRender();
    })
    $("#clear").click(function () {
        $(".search_input").val("");
    })

    function tableRender() {
        table.render({
            elem: '#demo'
            // , height: 312
            ,
            url: LOCALHOSTURL + '/team/team/getTeams' //数据接口
            ,
            method: "get",
            where: {
                "param": $("#param").val(),
                "leader": $("#leader").find("option:selected").val(),
                "coach": $("#coach").find("option:selected").val(),
                "start_date": $("#start_date").val(),
                "end_date": $("#end_date").val()
            },
            xhrFields: {
                withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
            },
            headers: {
                "Authorization": $.cookie("token")
            },
            crossDomain: true,
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,
                statusCode: 200 //规定成功的状态码，默认：0
            },
            page: true //开启分页
            ,
            title: "小组表",
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [ //表头
                    {
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                    field: 'id',
                    title: 'ID',
                    sort: true
                }, {
                    field: 'name',
                    title: '组名',
                    templet: function (res) {
                        if (res.name == null)
                            return '暂无';
                        else
                            return res.name;
                    }
                }, {
                    field: 'description',
                    title: '小组简介',
                    templet: function (res) {
                        if (res.description == null)
                            return '暂无';
                        else
                            return res.description;
                    }
                }, {
                    field: 'leader',
                    title: '组长',
                    templet: function (res) {
                        if (res.leaderName == null || res.leaderName == "")
                            if (res.leader != null && res.leader != "")
                                return res.leader;
                            else
                                return '暂无';
                        else
                            return res.leaderName;
                    }
                }, {
                    field: 'coach',
                    title: '教练',
                    templet: function (res) {
                        if (res.coachName == null || res.coachName == "") {
                            if (res.coach != null && res.coach != "")
                                return res.coach;
                            else
                                return '暂无'
                        } else
                            return res.coachName;
                    }
                }, {
                    field: 'stage',
                    title: '阶段',
                    templet: function (res) {
                        if (res.stage == null)
                            return '暂无';
                        else
                            return res.stage;
                    }
                }, {
                    field: 'clazzRoom',
                    title: '教室',
                    templet: function (res) {
                        if (res.clazzRoom == null)
                            return '暂无';
                        else
                            return res.clazzRoom;
                    }
                }, {
                    field: 'crew',
                    title: '小组人数',
                    sort: true,
                    templet: function (res) {
                        if (res.crew == null)
                            return '暂无';
                        else
                            return res.crew;
                    }
                }, {
                    field: 'createTime',
                    title: '成立时间',
                    templet: function (res) {
                        return res.createTime;
                    }
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150
                }
                ]
            ],
            done: function (res) {
                console.log(res);
            }
        });
    }

    //第一个实例

    table.on('toolbar(demo)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                layer.open({
                    type: 2,
                    title: "添加用户信息",
                    area: ['100%', '100%'],
                    content: ["editTeam.html"],
                    shadeClose: true, //点击旁边地方自动关闭
                    //动画
                    anim: 0,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                        var iframeWin = layero.find('iframe')[0].contentWindow;
                        // 获取子页面的iframe
                        // var iframe = window['layui-layer-iframe' + index];
                        iframeWin.editTeam("",index);
                    }
                })
                break;
        }
        ;
    });

    //监听行工具事件
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'manage') {
            layer.open({
                type: 2,
                title: "小组人员管理",
                area: ['100%', '100%'],
                content: ["TeamMember.html"],
                shadeClose: true, //点击旁边地方自动关闭
                //动画
                anim: 0,
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                    var iframeWin = layero.find('iframe')[0].contentWindow;
                    console.log(iframeWin);
                    iframeWin.setTeamMemberTransfer(JSON.stringify(data),index);
                }
            })
        } else if (obj.event === 'del') {
            layer.confirm("真的删除小组" + data.name + "?", function (index) {
                // obj.del();
                let result = myAjax("/team/team/deleteTeam", "get", {id: data.id});
                alert(result.message);
                tableRender();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            // layer.alert(JSON.stringify(obj.data));
            layer.open({
                type: 2,
                title: "修改小组信息",
                area: ['100%', '100%'],
                content: ["editTeam.html"],
                shadeClose: true, //点击旁边地方自动关闭
                //动画
                anim: 0,
                success: function (layero, index) {
                    console.log(index);
                    var body = layer.getChildFrame('body', index);
                    // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                    var iframeWin = layero.find('iframe')[0].contentWindow;
                    console.log(iframeWin);
                    iframeWin.editTeam(JSON.stringify(data),index);
                }
            })
        }
    });

    //获取码表数据
    function getLeader() {
        let data = null;
        // if (sessionStorage.getItem("leader") != null) {
        //     data = JSON.parse(sessionStorage.getItem("leader"));
        // } else {
            let result = ajaxModel("/user/user/getLeaderList", "get");
            data = result.data;
            sessionStorage.setItem("leader", JSON.stringify(data));
        // }
        console.log(data);
        $("#leader").html("<option value=''>全部</option>");
        $.each(data, function (i, leader) {
            if (leader.name != null)
                $("#leader").append("<option value='" + leader.code + "'>" + leader.code + "(" + leader.name + ")" + "</option>");
            else
                $("#leader").append("<option value='" + leader.code + "'>" + leader.code + "</option>");
        })
    }

    function getCoach() {
        let data = null;
        // if (sessionStorage.getItem("coach") != null) {
        //     data = JSON.parse(sessionStorage.getItem("coach"));
        // } else {
            let result = ajaxModel("/user/user/getCoachList", "get");
            data = result.data;
            sessionStorage.setItem("coach", JSON.stringify(data));
        // }
        console.log(data);
        $("#coach").html("<option value=''>全部</option>");
        $.each(data, function (i, coach) {
            if (coach.name != null && coach.name != '')
                $("#coach").append("<option value='" + coach.code + "'>" + coach.code + "(" + coach.name + ")" + "</option>");
            else
                $("#coach").append("<option value='" + coach.code + "'>" + coach.code + "</option>");
        });
    }
});
